﻿@using RuoVea.ExEnum
@using RuoVea.ExSugar
@using RuoVea.OmiApi.Article.Entitys
@using RuoVea.OmiApi.Article.Service
@using RuoVea.OmiApi.Article.Service.Dto
@inject ArticleService _articleService;
@{
    Layout = "_Layout";
    // 从URL中提取ID
    var currentPath = Context.Request.Path.Value;
    long? currentCategoryId = null;

    if (currentPath.StartsWith("/List/") && currentPath.EndsWith(".html"))
    {
        var idString = currentPath.Replace("/List/", "").Replace(".html", "");
        if (long.TryParse(idString, out var id))
        {
            currentCategoryId = id;
        }
    }
    int currentPageNo = Context.Request.Query.ContainsKey("pageNo") ? int.TryParse(Context.Request.Query["pageNo"], out var page) ? page : 1 : 1;
    var articleList = await _articleService.GetPagesAsync(new ArticleParam { CategorieIsDisable = YesOrNot.N, CategorieIds = new List<long> { currentCategoryId??0 }, PageNo = currentPageNo, PageSize = 10 });
    int totalPage = articleList.TotalRows == 0 ? 1 : (int)Math.Ceiling((double)articleList.TotalRows / articleList.PageSize);
    int currentPage = Math.Max(1, Math.Min(currentPageNo, totalPage));
}
@foreach (var c in articleList.Rows){
    <div class="cl-card">
        <div class="row">
            <div class="col-lg-3 col-md-4 col-sm-12 cl-card-image"><a href="@($"/Details/{c.Id}.html")"><img src="@c.CoverImage" class="img-fluid" alt=""></a></div>
            <div class="col-lg-9 col-md-8 col-sm-12 cl-card-main">
                <h3 class="cl-card-main-title"><a href="@($"/Details/{c.Id}.html")">@Html.Raw(c.Title)</a></h3>
                <p class="cl-card-main-info">@Html.Raw(c.Summary)</p>
                <p class="cl-card-more"><a href="@($"/Details/{c.Id}.html")">阅读更多...</a></p>
            </div>
        </div>
    </div>
}
<nav aria-label="Page navigation">
    <ul class="pagination justify-content-center">
        <li class="page-item @(currentPage == 1 ? "disabled" : "")">
            <a class="page-link" href="?pageNo=@(currentPage - 1)">&laquo;</a>
        </li>
        @for (int i = 1; i <= totalPage; i++)
        {
            <li class="page-item @(i == currentPage ? "active" : "")">
                <a class="page-link" href="?pageNo=@i">@i</a>
            </li>
        }
        <li class="page-item @(currentPage == totalPage ? "disabled" : "")">
            <a class="page-link" href="?pageNo=@(currentPage + 1)">&raquo;</a>
        </li>
    </ul>
</nav>
<div class="text-center text-muted small mt-2">
    @{ int startItem = (currentPage - 1) * articleList.PageSize + 1; int endItem = Math.Min(currentPage * articleList.PageSize, articleList.TotalRows); }
    显示 @startItem 到 @endItem 条，共 @articleList.TotalRows 条记录
</div>


